package com.alliky.core.base;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.alliky.core.R;

public abstract class BaseTitleActivity extends AppCompatActivity {

    protected TextView titleTv;
    protected TextView titleRight;
    private ImageView titleBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base_title);
        initBaseView();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View.inflate(this, layoutResID, (ViewGroup) findViewById(R.id.base_content));
    }

    /**
     * 调用后 才能得到titleTv否则为空
     */
    private void initBaseView() {
        setSupportActionBar((Toolbar) findViewById(R.id.base_tool_bar));
        titleTv = (TextView) findViewById(R.id.base_toolbar_title);
        titleBack = (ImageView) findViewById(R.id.base_nav_back);
        titleRight = (TextView) findViewById(R.id.base_nav_right);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        titleRight.setEnabled(false);
        BaseTitleClick baseTitleClick = new BaseTitleClick();
        titleBack.setOnClickListener(baseTitleClick);
        titleRight.setOnClickListener(baseTitleClick);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    public void back() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 设置中间标题
     *
     * @param titleText
     */
    public void setTitle(String titleText) {
        if (titleText != null) {
            if (titleTv != null) {
                titleTv.setText(titleText);
            }
        }
    }


    /**
     * @param text
     * @param drawableRes 设置右侧的按钮，可显示文字或图片
     */
    public void setTitleRight(String text, Drawable drawableRes) {
        if (titleRight == null) {
            return;
        }
        if (text == null && drawableRes == null) {
            titleRight.setVisibility(View.GONE);
        } else {
            titleRight.setVisibility(View.VISIBLE);
        }
        if (text != null) {
            titleRight.setText(text);
            titleRight.setBackgroundResource(R.color.transparent);
        }
        if (drawableRes != null) {
            titleRight.setBackgroundDrawable(drawableRes);
            titleRight.setText("");
        }

    }

    /**
     * 标题按钮的点击事件
     */
    private class BaseTitleClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.base_nav_back) {
                onBackClick();
            } else if (id == R.id.base_nav_right) {
                onRightClick();
            }
        }
    }

    /**
     * 标题中右边的部分，
     */
    protected void onRightClick() {
    }

    /**
     * 返回按钮的点击事件
     */
    private void onBackClick() {
        back();
    }

}